package com.study.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.mapper.LogMapper;
import com.study.mapper.UserMapper;
import com.study.mapper.UserRoleMapper;
import com.study.model.Logs;
import com.study.model.User;
import com.study.model.UserList;
import com.study.model.UserPoi;
import com.study.service.UserService;
import com.study.util.MyDate;
import com.study.util.getUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;

/**
 * @author king james
 * @create 2021-09-03-{TIME}
 */
@Service("userService")
public class UserServiceImpl extends BaseService<User> implements UserService{
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogMapper logMapper; //对日志记录

    @Override
    public PageInfo<User> selectByPage(User user, int start, int length) {
        int page = start/length+1;
//        Example example = new Example(User.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (StringUtil.isNotEmpty(user.getUsername())) {
//            criteria.andLike("username", "%" + user.getUsername() + "%");
//        }
//        if (user.getId() != null) {
//            criteria.andEqualTo("id", user.getId());
//        }
//        if (user.getEnable() != null) {
//            criteria.andEqualTo("enable", user.getEnable());
//        }
        //分页查询
        PageHelper.startPage(page, length);
//        List<User> userList1 = selectByExample(example);
        List<User> userList = userMapper.queryAllUserInfo(user);

        //记录日志
//        getUserInfo getUserInfo = new getUserInfo(); //用户信息
//        MyDate myDate = new MyDate();//当前系统时间
//        Logs logs = new Logs();
//        logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t查看了用户列表");
//        logs.setCreateTime(myDate.getDate());
//        logMapper.addLog(logs);

        return new PageInfo<>(userList);
    }

    /**
     * 查询用户
     * @param user
     * @param start
     * @param length
     * @return
     */
    @Override
    public PageInfo<UserList> queryAllUserInfo(User user, int start, int length) {
        int page = start/length+1;
        //分页查询
        PageHelper.startPage(page, length);
        List<UserList> userList = userMapper.queryAllUserInfo1(user);
         return new PageInfo<>(userList);
    }

    /**
     * 新增用户
     * @param username
     * @return
     */
    @Override
    public User selectByUsername(String username) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        List<User> userList = selectByExample(example);

        //记录日志
//        getUserInfo getUserInfo = new getUserInfo(); //用户信息
//        MyDate myDate = new MyDate();//当前系统时间
//        Logs logs = new Logs();
//       // logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t新增了一个用户--->" + username );
//        logs.setCreateTime(myDate.getDate());
//        logMapper.addLog(logs);
        if(userList.size()>0){
            return userList.get(0);
        }
            return null;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public void delUser(int[] userid) {
        //删除用户表数据
            //记录日志
            getUserInfo getUserInfo = new getUserInfo(); //用户信息
            MyDate myDate = new MyDate();//当前系统时间
            Logs logs = new Logs();
            for (int id:userid){
                String userName = userMapper.queryUserName(id);//获取被删除用户的姓名
                logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t删除了id为" + id +"的用户--->" + userName );
                logs.setCreateTime(myDate.getDate());
                logMapper.addLog(logs);
                //删除用户角色表
                userRoleMapper.deleteUserRole(id);
            }
           userMapper.deleteUserById(userid);

    }

    @Override
    public User detailUser(Integer userid) {
        //查看用户信息
        //获得用户详情
        User user = userMapper.queryUserById(userid);
        System.out.println(user.getUsername() + "用户名");
        return user;
    }

    /**
     * 启用用户
     * @param userid
     * @return
     */
    @Override
    public int enableUserById(Integer userid) {
        int flog = 0;
        flog = userMapper.enableUserById(userid);
        //表示启用用成功
        if (flog>0){
            //记录日志
            getUserInfo getUserInfo = new getUserInfo(); //用户信息
            MyDate myDate = new MyDate();//当前系统时间
            Logs logs = new Logs();
            String userName = userMapper.queryUserName(userid);//获取被启用的用户的姓名
            logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t启用了id为\t" +userid +"的用户----》" + userName);
            logs.setCreateTime(myDate.getDate());
            logMapper.addLog(logs);
            return 2;
        }
        else {
            return 3;
        }
    }

    /**
     * 禁用用户
     * @param userid
     * @return
     */
    @Override
    public int disableUserById(Integer userid) {
        int flog = 0;
        flog = userMapper.disableUserById(userid);
        //表示禁用成功
        if (flog>0){
            //记录日志
            getUserInfo getUserInfo = new getUserInfo(); //用户信息
            MyDate myDate = new MyDate();//当前系统时间
            Logs logs = new Logs();
            String userName = userMapper.queryUserName(userid);//获取被禁用的用户的姓名
            logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t禁用了id为\t" +userid +"的用户---》" + userName);
            logs.setCreateTime(myDate.getDate());
            logMapper.addLog(logs);
            return 2;
        }
        else {
            return 3;
        }
    }
   //查找用户状态
    @Override
    public int queryUserStatus(Integer userid) {
       return userMapper.queryUserStatus(userid);
    }


    /**
     * 导出用户表数据
     * @return
     */
    @Override
    public List<UserPoi> exportUserList() {
        return userMapper.exportUserList();
    }

    /**
     * 获取到用户的角色
     * @param username
     * @return
     */
    @Override
    public String getUserRole(String username) {
        return userMapper.getUserRole(username);
    }

    /**
     * 获取到新增用户id
     * @param username
     * @return
     */
    @Override
    public int getUserId(String username) {
        return userMapper.getUserId(username);
    }

    /**
     * 给用户设置默认角色 ：普通用户
     * @param userId
     * @return
     */
    @Override
    public int setUserRole(Integer userId) {
        return userMapper.setUserRole(userId);
    }
}
